package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.manager.activity.CrossGeneralActivityRankMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manager.yrank.CrossYRankCacheMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_CROSS_GENERAL_USER_MY_RANK, desc = "我的排名")
public class CrossGeneralActivityUserMyRankCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long userId = packet.getPlayerId();
        UnionActivityGroupProto.BaseUnionActivityGroupMsg baseMsg = UnionActivityGroupProto.BaseUnionActivityGroupMsg.parseFrom(packet.getBytes());
        int activityId = baseMsg.getActivityId();
        int groupId = baseMsg.getGroupId();

        CommonActivityProto.CommonActivityRankListReqMsg reqMsg = CommonActivityProto.CommonActivityRankListReqMsg.parseFrom(baseMsg.getMsg());
        int conditionType = reqMsg.getRankType();

        CommonActivityProto.CommonActivityRankListRespMsg.Builder respMsg = CommonActivityProto.CommonActivityRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if (activityInfo != null) {
            if (!UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
                CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, conditionType);
                if (rankListModel != null) {
                    respMsg.setMyRank(rankListModel.getMyRank(userId, serverId));
                    for (Long uid : reqMsg.getOtherUserIdList()) {
                        UserBaseInfo baseInfo = CrossUserMgr.getUserBaseInfo(uid);
                        if (baseInfo != null) {
                            respMsg.addOtherRank(rankListModel.getMyRank(uid, baseInfo.getServerId()));
                        }
                    }
                } else {
                    int rank = CrossGeneralActivityRankMgr.getCrossActivityRank(activityId, conditionType, serverId, userId);
                    respMsg.setMyRank(rank);
                }
            } else {
                String idKey = String.valueOf(conditionType);
                RankProto.RankListMsg.Builder rankBuilder = CrossYRankCacheMgr.getUserRankList(activityId, groupId, idKey);
                int myRank = CrossYRankCacheMgr.getUserRank(activityId, groupId, idKey, rankBuilder, userId);

                respMsg.setMyRank(myRank);
                for (Long uid : reqMsg.getOtherUserIdList()) {
                    int otherRank = CrossYRankCacheMgr.getUserRank(activityId, groupId, idKey, rankBuilder, uid);
                    respMsg.addOtherRank(otherRank);
                }
            }
        }

        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(Protocol.U_GENERAL_ACTIVITY_USER_RANK, respMsg));
    }

    @Override
    public void sendData() {

    }
}
